import 'package:flutter/material.dart';
import '../../l10n/app_localizations.dart';
import '../order/controller/order_detail.dart';

class OverlayView {
  static final OverlayView _instance = OverlayView._internal();
  factory OverlayView() => _instance;
  OverlayView._internal();

  static OverlayView get shared => _instance;

  final GlobalKey<_OverlayViewState> _key = GlobalKey<_OverlayViewState>();
  OverlayEntry? _overlayEntry;

  void show(BuildContext context,String orderId) {
    if (_overlayEntry != null) return;
    
    _overlayEntry = OverlayEntry(
      builder: (context) => _OverlayView(key: _key),
    );
    
    Overlay.of(context).insert(_overlayEntry!);
    
    Future.delayed(Duration(seconds: 3), () {
      hide();
      Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => OrderDetailPage(orderId: orderId),
      ),
    );
    });
  }

  void hide() {
    _overlayEntry?.remove();
    _overlayEntry = null;
  }
}

class _OverlayView extends StatefulWidget {
  const _OverlayView({Key? key}) : super(key: key);

  @override
  _OverlayViewState createState() => _OverlayViewState();
}

class _OverlayViewState extends State<_OverlayView> {
  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned.fill(
          child: Container(
            color: Colors.white,
          ),
        ),
        Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              CircularProgressIndicator(
                valueColor: AlwaysStoppedAnimation<Color>(Colors.blue),
                strokeWidth: 4,
              ),
              SizedBox(height: 20),
              Text(
                AppLocalizations.of(context)!.qingqiuhuidiao,
                style: TextStyle(
                  color: Colors.blue,
                  fontSize: 18,
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }
}